package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.UserHolder;
import com.itheima.ydd.dto.SysUserDto;
import com.itheima.ydd.entity.*;
import com.itheima.ydd.mapper.SysUserMapper;
import com.itheima.ydd.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {
    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ISysMenuService sysMenuService;

    @Autowired
    private ISysRoleMenuService iSysRoleMenuService;

    @Autowired
    private ISysDictService sysDictService;

    @Override
    public Map<String, Object> profile() {
        HashMap<String, Object> map = new HashMap<>();
        //1. 查询用户信息
        String userId = UserHolder.getUserId();
        SysUser user = sysUserService.getById(UserHolder.getUserId());
        SysUserDto userInfo = new SysUserDto();
        BeanUtils.copyProperties(user, userInfo);

        //查询用户角色id集合: role_id
        String[] rowIds = user.getRoleId().replace("[", "")
                .replace("]", "")
                .replace("\"", "")
                .split(",");
        List<String> ids = new ArrayList<>();
        Collections.addAll(ids, rowIds);
        userInfo.setRoleIds(ids);
        String role_id = UserHolder.get().getRoleId();
        String[] strings = role_id.split(",");
        LambdaQueryWrapper<SysRole> lqw = new LambdaQueryWrapper();
        lqw.in(SysRole::getId, strings);
        List<SysRole> list = this.sysRoleService.list(lqw);
        List<String> roleNameList = (List) list.stream().map((sysRole) -> {
            return sysRole.getName();
        }).collect(Collectors.toList());
        userInfo.setRolename(roleNameList);
        map.put("userInfo", userInfo);
        //2. 查询角色菜单集合
        LambdaQueryWrapper<SysRoleMenu> lqw2 = new LambdaQueryWrapper();
        lqw2.in(SysRoleMenu::getRoleId, strings);
        List<SysRoleMenu> list1 = this.iSysRoleMenuService.list(lqw2);
        List<String> collect = list1.stream().map(sysRoleMenu -> sysRoleMenu.getMenuId()
        ).collect(Collectors.toList());
        LambdaQueryWrapper<SysMenu> lqw3 = new LambdaQueryWrapper();
        lqw3.in(SysMenu::getId, collect);
        lqw3.ne(SysMenu::getPath, "");
        List<SysMenu> menuList = this.sysMenuService.list(lqw3);
        map.put("menuList", menuList);


        //region #dictsList 不知道干嘛用的，结果直接为原本数据
        HashMap<String, Object> dictsList = new HashMap<>();
        HashMap<String, String> interface_type = new HashMap<>();
        interface_type.put("get", "get");
        interface_type.put("post", "post");
        dictsList.put("interface_type", interface_type);

        ArrayList<String> table_type = new ArrayList<>();
        table_type.add("单表");
        table_type.add("主表");
        table_type.add("附表");
        table_type.add("树结构表");
        dictsList.put("table_type", table_type);

        ArrayList<String> order_type = new ArrayList<>();
        order_type.add("支付宝");
        order_type.add("微信");
        dictsList.put("order_type", order_type);

        ArrayList<String> report_type = new ArrayList<>();
        report_type.add("正常报告");
        report_type.add("异常报告");
        dictsList.put("report_type", report_type);

        HashMap<String, String> theme = new HashMap<>();
        theme.put("ace", "ACE风格");
        dictsList.put("theme", theme);
        map.put("dictsList", dictsList);

        return map;
    }
}